//
//  UnpayViewController.m
//  KiklinkApp
//
//  Created by LJC on 14-9-15.
//  Copyright (c) 2014年 Kiklink. All rights reserved.
//

#import "UnpayViewController.h"

#import "ImagesManager.h"
#import "UIColor+hex.h"
#import "UIView+loadWithNib.h"

#import "MJRefresh.h"
#import "UnpayCell.h"

#import "API.h"

NSString *const unpayTableCellIdentifier = @"UnpayCell";

@interface UnpayViewController ()
{
    //data
    NSMutableArray *unpayTableArray;
    NSInteger page;
    NSInteger totalPage;
}
@end

@implementation UnpayViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"未完成订单";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    
    
    //loading
    [self.activity startAnimating];
    self.refreshBtn.hidden = YES;
    [self.refreshBtn addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
    
    
    //BundleImages
    ImagesManager *imagesManager = [ImagesManager sharedImagesManager];
    
    
    //leftNavItem
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(0, 0, NavBarHeight/4, NavBarHeight/2)];
    [backButton setBackgroundImage:[imagesManager back] forState:UIControlStateNormal];
    [backButton setBackgroundImage:[imagesManager backTapped] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftNavBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftNavBarItem;
    
    
    //unpayTable
    self.unpayTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.unpayTable.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    self.unpayTable.delegate = self;
    self.unpayTable.dataSource = self;
    self.unpayTable.showsVerticalScrollIndicator = NO;
    [self.unpayTable registerClass:[UITableViewCell class] forCellReuseIdentifier:unpayTableCellIdentifier];
    [self setupRefresh];
    
    
    //data
    unpayTableArray = [[NSMutableArray alloc] init];
    page = 1;
    [self getData:1 Condition:@"first"];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - get data
//condition:first&header&footer
- (void)getData:(NSInteger)pageNum Condition:(NSString *)condition
{
    NSLog(@"page:%ld",(long)pageNum);
    [API unpayedOrderPageWithPageNumber:[NSNumber numberWithInteger:pageNum] Callback:^(int stateCode, NSString *word, id content) {
        
        if (stateCode == SUCCESS) {
            
            OrderPage *orderPage = (OrderPage *)content;
            NSMutableArray *result = [[NSMutableArray alloc] initWithArray:orderPage.result];
            
            if ([condition isEqualToString:@"first"]) {
                totalPage = orderPage.lastPageNumber;
                self.unpayTable.scrollEnabled = YES;
                page = 2;
                unpayTableArray = result;
            } else if ([condition isEqualToString:@"header"]) {
                page = 2;
                unpayTableArray = result;
            } else if ([condition isEqualToString:@"footer"]) {
                page ++;
                [unpayTableArray addObjectsFromArray:result];
            }
            
            [self loadData:condition];
            
        } else {
            
            if ([condition isEqualToString:@"first"]) {
                self.unpayTable.scrollEnabled = NO;
                [self.activity stopAnimating];
                self.refreshBtn.hidden = NO;
            } else if ([condition isEqualToString:@"header"]) {
                [self.unpayTable headerEndRefreshing];
            } else if ([condition isEqualToString:@"footer"]) {
                [self.unpayTable footerEndRefreshing];
            }
            
        }
        
    }];
    
}

- (void)loadData:(NSString *)condition
{
    [self.unpayTable reloadData];
    
    if ([condition isEqualToString:@"first"]) {
        [self.activity stopAnimating];
    } else if ([condition isEqualToString:@"header"]) {
        [self.unpayTable headerEndRefreshing];
    } else if ([condition isEqualToString:@"footer"]) {
        [self.unpayTable footerEndRefreshing];
    }
}

- (void)refresh
{
    self.refreshBtn.hidden = YES;
    [self.activity startAnimating];
    
    [self getData:1 Condition:@"first"];
}
#pragma mark - get data End

#pragma mark - back
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - back End

#pragma tableView dataSource&delegate  &MJRefresh
- (void)setupRefresh
{
    [self.unpayTable addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.unpayTable addFooterWithTarget:self action:@selector(footerRereshing)];
    
    self.unpayTable.headerPullToRefreshText = @"下拉可以刷新";
    self.unpayTable.headerReleaseToRefreshText = @"松开马上刷新";
    self.unpayTable.headerRefreshingText = @"正在刷新...";
    self.unpayTable.footerPullToRefreshText = @"上拉加载更多";
    self.unpayTable.footerReleaseToRefreshText = @"松开马上加载";
    self.unpayTable.footerRefreshingText = @"正在加载...";
}
- (void)headerRereshing
{
    //data
    [self getData:1 Condition:@"header"];
}
- (void)footerRereshing
{
    //data
    if (page <= totalPage) {
        [self getData:page Condition:@"footer"];
    } else {
        [self.unpayTable footerEndRefreshing];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return unpayTableArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BOOL nibsRegistered = NO;
    if (!nibsRegistered) {
        UINib *nib = [UINib nibWithNibName:@"UnpayCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:unpayTableCellIdentifier];
        nibsRegistered = YES;
    }
    UnpayCell *cell = (UnpayCell *)[tableView dequeueReusableCellWithIdentifier:unpayTableCellIdentifier];
    [cell loadDataWithOrderHeader:[unpayTableArray objectAtIndex:indexPath.row]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 187;
}
#pragma tableView dataSource&delegate End

@end
